﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRSTLWriter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

public class CApExportToRTRSTLWriter
{
  internal const string STL_SUFFIX = ".stl";
  internal const string JT_SUFFIX = ".jt";
  internal const string EMPTY_LINK = "emptyLink";
  private ITxObject m_obj;
  public static Dictionary<string, string> Prototypes = new Dictionary<string, string>();

  public CApExportToRTRSTLWriter(ITxObject obj) => this.m_obj = obj;

  public void Save(string path)
  {
    if (string.IsNullOrEmpty(path))
      throw new ArgumentNullException("STLWriter: Path is empty");
    if (!Directory.Exists(Path.GetDirectoryName(path)))
      Directory.CreateDirectory(Path.GetDirectoryName(path));
    switch (Path.GetExtension(path).ToLower())
    {
      case ".stl":
        this.WriteSTL(path);
        break;
      case ".jt":
        this.WriteJt(path);
        break;
    }
  }

  public static string GetStlFileName(ITxObject obj, bool makeUnique = true, string suffix = ".stl")
  {
    string stlFileName = string.Empty;
    if (obj != null)
    {
      string source = string.Join("_", obj.Name.Replace("/", "_").Split(Path.GetInvalidFileNameChars()));
      if (suffix == ".jt")
      {
        char[] array = source.Where<char>((Func<char, bool>) (c => !char.IsLetterOrDigit(c) && c != '_')).ToArray<char>();
        source = string.Join("_", source.Split(array));
      }
      stlFileName = !makeUnique ? $"{source}{suffix}" : $"{source}_{obj.Id.Replace(",", "_")}{suffix}";
    }
    return stlFileName;
  }

  private void WriteSTL(string path)
  {
    TxExportGraphicsAnimationInitData animationInitData = new TxExportGraphicsAnimationInitData(this.m_obj, path, "", false, CApExportToRTRExportManager.LOD_VALUE, false);
    animationInitData.ExportData = new TxExportGraphicsAnimationData();
    animationInitData.FileFormat = (TxExportGraphicsAnimationFileFormatEx) 5;
    animationInitData.UseWorkingFrame = CApExportToRTRExportManager.UseWorkingFrame;
    TxExportGraphicsAnimationEx.Init(animationInitData);
    animationInitData.Dispose();
  }

  private void WriteJt(string path)
  {
    TxObjectList dataToExport;
    if (!(this.m_obj is TxKinematicLink))
    {
      TxObjectList txObjectList = new TxObjectList();
      ((Collection<ITxObject>) txObjectList).Add(this.m_obj);
      dataToExport = txObjectList;
    }
    else
      dataToExport = (this.m_obj as TxKinematicLink).LinkApprox();
    this.ExportToJt(dataToExport, path, this.m_obj is TxKinematicLink);
  }

  private void ExportToJt(TxObjectList dataToExport, string path, bool addRootNode = false)
  {
    path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
    new TxDocumentEx().SaveCollectionToJtAssembly(dataToExport, new TxJtExportDataEx(path)
    {
      TargetJtFormat = (TxJtExportDataEx.TxJtFormatEx) 0,
      ShouldAddRootNode = addRootNode,
      RootLocationType = addRootNode ? (TxJtExportDataEx.TxLocationType) 1 : (TxJtExportDataEx.TxLocationType) 0,
      TargetJtVersion = (TxJtVersionDataEx.TxJtVersionEx) 3
    });
  }
}
